<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="125px">
      <el-form-item label="姓名" prop="storeName">
        <el-input v-model="queryParams.storeName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="学员编号" prop="storeCode">
        <el-input v-model="queryParams.storeCode" placeholder="请输入学员编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="课程分类" prop="payGroupId">
        <el-input v-model="queryParams.payGroupId" placeholder="请输入课程分类" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="StudentList" @selection-change="handleSelectionChange">
      <el-table-column label="标识" align="center" prop="id" widt="150"/>
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="学员编号" align="center" prop="code" />
      <el-table-column label="派单交付小组" align="center" prop="payGroupName" />
      <el-table-column label="课程分类名称" align="center" prop="classTypeName" />
      <el-table-column label="期望上课时间" align="center" prop="expectTime" />
      <el-table-column label="派单时间" align="center" prop="dispatchTime" />
      <el-table-column label="派单备注" align="center" prop="dispatchRemark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button  v-if="!scope.row.payGroupName" size="mini" type="text" icon="el-icon-edit" @click="getOpen(scope.row)"
            v-hasPermi="['course:StoreGroup:edit']">手动派单</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['course:StoreGroup:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog title="派单" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form"   :rules="rules" label-width="125px">
        <el-form-item label="学员编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入学员编号" disabled />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" disabled />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" disabled />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderCode">
          <el-input v-model="form.orderCode" placeholder="请输入订单号" disabled />
        </el-form-item>
        <el-form-item label="交付中心" prop="orderCode">
          <el-input v-model="form.payCenterName" placeholder="交付中心" disabled />
        </el-form-item>
        <el-form-item label="交付小组" prop="orderCode">
          <el-input v-model="form.payGroupName" placeholder="交付小组" disabled />
        </el-form-item>
        <el-form-item   label="交付中心" prop="payCenterId">
          <el-select @change="changeGroup" v-model="form.payCenterId" placeholder="请选择交付中心" filterable size="large">
            <el-option v-for="item in userList" value-key="item.userId" :key="item.userId" :label="item.userName"
              :value="item.userId" />
          </el-select>
        </el-form-item>
        <el-form-item label="交付小组" prop="payGroupId">
          <el-select v-model="form.payGroupId" placeholder="请选择交付小组" filterable size="large">
            <el-option v-for="item in PayGroupList" value-key="item.id" :key="item.id" :label="item.name"
              :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="dispatchRemark">
          <el-input v-model="form.dispatchRemark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listStoreGroup, } from "@/api/course/StoreGroup";
import { listStudent, } from "@/api/course/Student";
import { listDispatch, getDispatch, delDispatch, addDispatch, updateDispatch } from "@/api/course/Dispatch";
import { findAllByRole } from "@/api/system/user";
import { listPayGroup } from "@/api/course/PayGroup";
import store from '@/store'


export default {
  name: "StoreGroup",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 交付小组列表
      CoachList: [],
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        classTypeId: null,
      },
      // 学员信息表格数据
      StudentList: [],
      // 用户列表
      userList: [],
      PayGroupList: [],
      // 总条数
      total: 0,
      rules: {
        payCenterId: [
          { required: true, message: "交付中心不能为空", trigger: "blur" }
        ],
        payGroupId: [
          { required: true, message: "交付小组不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学员信息列表 */
    getList() {
      this.loading = true;
      //是否试课：0试课  1正式课
      const isTest = this.$route.query.isTest;
      // console.log('[ isTest ] >', isTest)
      this.queryParams.isTest = isTest;
      listStudent(this.queryParams).then(response => {
        this.StudentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      findAllByRole({ roleName:'交付中心'}).then(response => {
        this.userList = response.data;
      });

    },
    changeGroup() {
      console.log('[ gggg` ] >', this.form.payCenterId)
      listPayGroup({ pageNum: 1, pageSize: 999, createBy: this.form.payCenterId }).then(response => {
        this.PayGroupList = response.rows;
      });
    },
    getOpen(row) {
      this.form = row;
      if(row.payCenterId){
        this.form.payCenterId = Number(row.payCenterId)
        this.changeGroup()
      }
      this.open = true;

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        phone: null,
        orderCode: null,
        centerId: null,
        payGroupId: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm() {
      this.form.studentId = this.form.id;
      this.form.centerId = this.form.payCenterId;
      this.form.id = null;
      console.log('[ form ] >', this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDispatch(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDispatch(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>
